from pydoc import replace

import scipy.stats as stats
import pandas as pd
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
import seaborn as sns
# import matplotlib
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
import matplotlib.dates as mdates
from matplotlib.colors import LogNorm


data=pd.read_csv('E:\江苏移动-hzy工作资料\\5GC学习资料\\01-中兴5GC资料\python-tasks\show interface brief.csv')

df = data.loc[:, ['run_time', 'host_ip','Interface','BW(Mbps)']]
df.columns = ['time', 'host_ip','Interface','data']

df['data']=df['data']+range(0,len(df['data']))
start_date = pd.to_datetime('2022-01-01 01:00:00')
end_date = pd.to_datetime('2022-12-31')

# date_list = pd.date_range(start_date, end_date, freq='D').strftime('%Y-%m-%d').tolist()
date_list = pd.date_range(start=start_date, periods=len(df), freq='Min').strftime('%Y-%m-%d %H:%M:%S').tolist()
df['time']=date_list
# df['time']=pd.to_datetime(date_list)

df['label']=df['host_ip']+' : '+df['Interface']
# df.set_index('time',inplace=True])
df=df[['time','label','data']]

matrix = df.pivot(index='time', columns='label', values='data')


# plt.style.use('seaborn-bright')
rcParams['font.sans-serif'] = ['SimHei']  # 中文为宋体
rcParams['font.serif'] = ['Times New Roman']  # 英文为新罗马
rcParams['axes.unicode_minus'] = False  # 正常显示负号
rcParams['font.size'] = 15  # 设置字号

# cm = chaos_matrix(true_labels, predictions, labels=label_names)
# df_cfm = pd.DataFrame(cm, index=label_names, columns=label_names)
fig=plt.figure(figsize=(18, 6), dpi=300,constrained_layout=True)  # 可以自动调整图表之间的距离大小
spec=plt.GridSpec(1,1,figure=fig)


ax_main = fig.add_subplot(spec[:, :])
# cell_value = df_cfm.applymap(lambda v: v if v else  "" )
# cfm_plot = sns.heatmap(matrix, annot=matrix.values, fmt= "" , norm=LogNorm(), linewidths= 0.5 , linecolor= "grey" , cmap= "crest" )
cfm_plot = sns.heatmap(matrix, ax=ax_main, fmt= "" , norm=LogNorm(), linewidths= 0.5 , linecolor= "grey" , cmap= "crest" )
# ax_main.set_xticks(df['time'])

#
# # 设置主刻度格式
# # hoursLoc = mdates.HourLocator(interval=1)  # 为1小时为1主刻度
# minute3 =  mdates.MinuteLocator(interval=3)
# ax_main.xaxis.set_major_locator(minute3)
# ax_main.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M\n%Y-%m-%d'))
#
# # 设置副刻度格式
# minute1 =  mdates.MinuteLocator(interval=1)
# # minute = mdates.MinuteLocator(byminute=[0, 10, 20, 30, 40, 50])
#
# ax_main.xaxis.set_minor_locator(minute1) # 设置x轴刻度，每10分钟显示一个
# ax_main.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))
#
#

plt.setp(ax_main.xaxis.get_majorticklabels(), rotation=50)
plt.setp(ax_main.yaxis.get_majorticklabels(), rotation=0)
# # 设置主刻度旋转角度和刻度label刻度间的距离pad
# ax_main.tick_params(which='major', axis='x', length=5, pad=4, direction='in',labelsize=11)
# ax_main.tick_params(which='minor', axis='x', direction='in', labelsize=10.5)
ax_main.tick_params(axis='x', direction='in', labelsize=12)
ax_main.tick_params(axis='y', direction='in', labelsize=12,pad=2)

# # ax_main.xaxis.grid(True, which='major',linestyle='--',linewidth=0.5,color='gray',zorder=0)#zorder=0标识置于底层
# # ax_main.xaxis.grid(True, which='minor',linestyle='--',linewidth=0.5,color='gray',zorder=0)#zorder=0标识置于底层
# # ax_main.yaxis.grid(True, which='major',linestyle='--',linewidth=0.5,color='gray',zorder=0)#zorder=0标识置于底层

plt.show()










